//
//  AuthorBlindViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/10/2.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "AuthorBlindViewController.h"
#import "BasicWebViewController.h"

@interface AuthorBlindViewController ()
@property (nonatomic, copy) NSDictionary *style;
@end

@implementation AuthorBlindViewController

- (NSDictionary *)style
{
    if (!_style) {
        _style = [NSDictionary dictionary];
        _style = @{
                   @"black24":@[titleColor,[UIFont fontWithName:@"DINAlternate-Bold" size:24]],
                   @"black12":@[titleColor,FontWithSize(12)]
                   };
    }
    return _style;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.bgNavView.hidden = YES;
   // [self changeThisShopState];
}

- (void)changeThisShopState
{
    //0 表示正常店铺，10：待审核， 30： 待小程序授权（响应结果中的扩展字段attributes中会放入 auth_url 字段即授权链接）
    [[HttpRequest currentHttpRequest]postPath:ShopState_API params:nil resultBlock:^(id Data, MsgModel *msgModel) {
        NSString *shopStatus = [NSString stringWithFormat:@"%@",Data[@"storeStatusCode"]];
        //保存店铺状态
        if (![shopStatus isEqualToString:USER_DEFAULTS_GET(StoreStatue)]) {
            USER_DEFAULTS_SAVE(shopStatus, StoreStatue);
        }
        if ([shopStatus isEqualToString:@"30"]) {
            NSDictionary *dict = Data[@"storeStatusExtend"];
            self.auth_url = [NSString stringWithFormat:@"%@",dict[@"auth_url"]];
            self.about_miniapp_url = [NSString stringWithFormat:@"%@",dict[@"about_miniapp_url"]];
            
        }else if([shopStatus isEqualToString:@"0"])
        {
            MYHTabBarController *tab = [MYHTabBarController new];
            AppDelegate* app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            app.window.rootViewController = tab;
        }else if ([shopStatus isEqualToString:@"10"])
        {
            MYHTabBarController *tab = [MYHTabBarController new];
            tab.selectedIndex = 2;
            AppDelegate* app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            app.window.rootViewController = tab;
        }
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *headIV = [UIImageView new];
    headIV.frame = CGRectMake(0, 0, SCREENWIDTH, 220);
    headIV.image = [UIImage imageNamed:@"Group"];
    [self.view addSubview:headIV];
    
    CGFloat left = (SCREENWIDTH-208)/2;
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(left, headIV.bottom+27, 208, 42);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"bindBtn"] forState:UIControlStateNormal];
    backBtn.titleLabel.textColor = titleColor;
    backBtn.titleLabel.font = FontSemiboldSize(12);
    [backBtn setTitle:@"复制链接" forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(copyStr:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    
    UIView *lineV = [UIView new];
    lineV.frame = CGRectMake(0, backBtn.bottom+28, SCREENWIDTH, 1);
    lineV.backgroundColor = UIColorFromHex(0xF0F0F0);
    [self.view addSubview:lineV];
    
    UILabel *titleLab = [UILabel new];
    titleLab.frame = CGRectMake(40, lineV.bottom+40, 100, 30);
    titleLab.textColor = titleColor;
    titleLab.font = FontSemiboldSize(22);
    titleLab.text = @"授权步骤";
    [self.view addSubview:titleLab];
    
    UIView *level1 = [UIView new];
    level1.frame = CGRectMake(40, titleLab.bottom+15, 60, 60);
    level1.layer.cornerRadius = 4;
    level1.layer.masksToBounds = YES;
    level1.backgroundColor = goldColor;
    [self.view addSubview:level1];
    
    UILabel *label1 = [UILabel new];
    label1.frame = CGRectMake(0, 0, level1.width, level1.height);
    label1.numberOfLines = 2;
    label1.attributedText = [[NSString stringWithFormat:@"<black24>%@\n</black24><black12>%@</black12>",@"1",@"步 骤"]attributedStringWithStyleBook:self.style];
    label1.textAlignment = NSTextAlignmentCenter;
    [level1 addSubview:label1];
    
    UILabel *desLab1 = [UILabel new];
    desLab1.frame = CGRectMake(level1.right+40, level1.top+20, 200, 20);
    desLab1.textColor = UIColorFromHex(0x4D4D4D);
    desLab1.font = FontSemiboldSize(14);
//    desLab1.text = @"复制链接，在微信中打开";
    desLab1.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"复制链接，在微信中打开" andColor:UIColorFromHex(0x4D4D4D)];
    [self.view addSubview:desLab1];
    
    UIView *level2 = [UIView new];
    level2.frame = CGRectMake(40, level1.bottom+15, 60, 60);
    level2.layer.cornerRadius = 4;
    level2.layer.masksToBounds = YES;
    level2.backgroundColor = goldColor;
    [self.view addSubview:level2];
    
    UILabel *label2 = [UILabel new];
    label2.frame = CGRectMake(0, 0, level2.width, level2.height);
    label2.numberOfLines = 2;
    label2.attributedText = [[NSString stringWithFormat:@"<black24>%@\n</black24><black12>%@</black12>",@"2",@"步 骤"]attributedStringWithStyleBook:self.style];
    label2.textAlignment = NSTextAlignmentCenter;
    [level2 addSubview:label2];
    
    UILabel *desLab2 = [UILabel new];
    desLab2.frame = CGRectMake(level2.right+40, level2.top+20, 200, 20);
    desLab2.textColor = UIColorFromHex(0x4D4D4D);
    desLab2.font = FontSemiboldSize(14);
    //desLab2.text = @"点击授权您开通的小程序";
    desLab2.attributedText = [TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"点击授权您开通的小程序" andColor:UIColorFromHex(0x4D4D4D)];
    [self.view addSubview:desLab2];
    
    UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (@available(iOS 11.0, *)) {
        bottomBtn.frame = CGRectMake(0, SCREENHEIGHT-50-BottomHeight_xs, SCREENWIDTH, 50);
    } else {
        // Fallback on earlier versions
        bottomBtn.frame = CGRectMake(0, SCREENHEIGHT-50-BottomHeight, SCREENWIDTH, 50);
    }
    bottomBtn.backgroundColor = TotalWhite;
    bottomBtn.layer.shadowColor = goldColor.CGColor;
    bottomBtn.layer.shadowOffset = CGSizeMake(0, -1);
    bottomBtn.layer.shadowRadius = 0;
    bottomBtn.layer.shadowOpacity = 0.5;
    [bottomBtn setTitleColor:titleColor forState:UIControlStateNormal];
    bottomBtn.titleLabel.font = FontWithSize(14);
    [bottomBtn setAttributedTitle:[TTKUtil getAtttributeStringWithLineSpace:0 kern:1 and:@"还没注册小程序？"] forState:UIControlStateNormal];
    [bottomBtn addTarget:self action:@selector(registBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bottomBtn];
}

#pragma mark -- 复制链接
- (void)copyStr:(UIButton *)btn
{
    UIPasteboard *pasteB = [UIPasteboard generalPasteboard];
    pasteB.string = self.auth_url;
    NSLog(@"url = %@",pasteB.string);
    if (pasteB.string == nil) {
        [self ShowHUDWithMessage:@"复制失败"];
    }else
    {
        [self ShowHUDWithMessage:@"复制成功"];
    }
}

#pragma mark -- 跳转网页
- (void)registBtn:(UIButton *)btn
{
    WebViewController *webView = [WebViewController new];
//    webView.titleName = @"测试title";
    webView.urlString = self.about_miniapp_url;
    webView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webView animated:YES];
}

@end
